package com.paipaideli.ui.mine.order.adpater;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.airbnb.epoxy.EpoxyAdapter;
import com.airbnb.epoxy.EpoxyHolder;
import com.airbnb.epoxy.EpoxyModelWithHolder;
import com.jakewharton.rxbinding2.view.RxView;
import com.paipaideli.R;
import com.paipaideli.common.utils.ImageUtil;
import com.paipaideli.ui.mine.order.OrderDetailActivity;
import com.paipaideli.ui.mine.order.bean.OrderBean;

import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019/9/29.
 */

public class OrderAdapter extends EpoxyAdapter {

    public void setDate(List<OrderBean.Order> list) {
        for (int i = 0; i < list.size(); i++) {
            OrderAdapterModel orderAdapterModel = new OrderAdapterModel();
            orderAdapterModel.setData(list.get(i));
            models.add(orderAdapterModel);
        }

    }

    public void clear() {
        models.clear();
    }

    class OrderAdapterModel extends EpoxyModelWithHolder<OrderAdapterModel.ViewHolder> {

        OrderBean.Order order;

        public void setData(OrderBean.Order order) {
            this.order = order;
        }


        @Override
        protected ViewHolder createNewHolder() {
            return new ViewHolder();
        }

        @Override
        protected int getDefaultLayout() {
            return R.layout.item_order;
        }

        @Override
        public void bind(@NonNull ViewHolder viewHolder) {
            super.bind(viewHolder);

            viewHolder.tvOrderGys.setText(order.supplierName);
            String status = order.status;
            if (status.equals("1")) {
                viewHolder.tvOrderDdzt.setText("待付款");
                viewHolder.btnOrderZt.setText("付款");
//                viewHolder.lin_order_zt.setVisibility(View.VISIBLE);
            } else if (status.equals("2")) {
                viewHolder.tvOrderDdzt.setText("待发货");
                viewHolder.btnOrderZt.setText("提醒发货");
//                viewHolder.lin_order_zt.setVisibility(View.VISIBLE);
            } else if (status.equals("3")) {
                viewHolder.tvOrderDdzt.setText("待收货");
                viewHolder.btnOrderZt.setText("确认收货");
//                viewHolder.lin_order_zt.setVisibility(View.VISIBLE);
            } else if (status.equals("4")) {
                viewHolder.tvOrderDdzt.setText("已完成");
//                viewHolder.lin_order_zt.setVisibility(View.GONE);
            } else {
                viewHolder.tvOrderDdzt.setText("已失效");
//                viewHolder.lin_order_zt.setVisibility(View.GONE);
            }
            try {
                ImageUtil.displayImage(order.goodsImg, viewHolder.imageOrderPic);
            } catch (Exception e) {
                e.getMessage();
            }
            viewHolder.tvOrderGoodname.setText(order.goodsName);
            viewHolder.tvOrderCjprice.setText("成交价:  ￥" + order.price);
            viewHolder.tvOrderYfprice.setText("邮费:  ￥" + order.freight);
            double zj = Double.valueOf(order.price) + Double.valueOf(order.freight);
            viewHolder.tvOrderZjprice.setText("共计：  ￥" + String.format("%.2f", zj) + "");

            //隐私权限的选中
            RxView.clicks(viewHolder.llOrder)
                    .throttleFirst(1, TimeUnit.SECONDS)
                    .subscribe(o -> {

                        Intent intent = new Intent(viewHolder.llOrder.getContext(), OrderDetailActivity.class);
                        intent.putExtra("orderId", order.id);
                        viewHolder.llOrder.getContext().startActivity(intent);
                    });
        }

        public class ViewHolder extends EpoxyHolder {
            @BindView(R.id.tv_order_gys)
            TextView tvOrderGys;
            @BindView(R.id.tv_order_ddzt)
            TextView tvOrderDdzt;
            @BindView(R.id.image_order_pic)
            ImageView imageOrderPic;
            @BindView(R.id.tv_order_goodname)
            TextView tvOrderGoodname;
            @BindView(R.id.tv_order_cjprice)
            TextView tvOrderCjprice;
            @BindView(R.id.tv_order_yfprice)
            TextView tvOrderYfprice;
            @BindView(R.id.tv_order_zjprice)
            TextView tvOrderZjprice;
            @BindView(R.id.btn_order_zt)
            Button btnOrderZt;
            @BindView(R.id.ll_order)
            LinearLayout llOrder;
            @BindView(R.id.lin_order_zt)
            LinearLayout lin_order_zt;

            @Override
            protected void bindView(View itemView) {
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
